package com.mdhw.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdhw.admin.dao.AdminRoleDao;
import com.mdhw.admin.entity.AdminMenu;
import com.mdhw.admin.entity.AdminRole;
import com.mdhw.admin.entity.AdminRoleMenu;
import com.mdhw.admin.service.AdminMenuService;
import com.mdhw.admin.service.AdminRoleMenuService;
import com.mdhw.admin.service.AdminRoleService;
import com.mdhw.admin.vo.AdminMenuTree;
import com.mdhw.admin.vo.AdminRoleVo;
import com.mdhw.common.base.PageData;
import com.mdhw.common.enums.StatusEnum;
import com.mdhw.config.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【admin_role】的数据库操作Service实现
* @createDate 2024-12-04 13:45:58
*/
@Service
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleDao, AdminRole> implements AdminRoleService{

    @Autowired
    private AdminRoleDao adminRoleDao;
    @Autowired
    private AdminRoleMenuService adminRoleMenuService;
    @Autowired
    private AdminMenuService adminMenuService;
    @Override
    public R<PageData<AdminRoleVo>> selectRoleVoPage(Long pageNo, Long pageSize, String name, String code) {
        Page page = new Page(pageNo,pageSize);
        QueryWrapper<AdminRoleVo> srvQw = new QueryWrapper<>();
        if(StringUtils.isNotBlank(name)){
            srvQw.eq("name",name);
        }
        if(StringUtils.isNotBlank(code)){
            srvQw.eq("code",code);
        }
        srvQw.eq("status", StatusEnum.OK.getCode());
        this.adminRoleDao.selectAdminRoleVoListPage(page,srvQw);
        PageData pageData = PageData.plusPageToPageData(page);
        return R.success(pageData);
    }

    @Override
    public R<String> addRole(AdminRole adminRole) {
        String code = adminRole.getCode();
        if(code == null || StringUtils.isBlank(code)){
            return R.error("code 不允许为空");
        }
        LambdaQueryWrapper<AdminRole> srLqw = new LambdaQueryWrapper<>();
        srLqw.eq(AdminRole::getStatus,StatusEnum.OK.getCode());
        srLqw.eq(AdminRole::getCode,code);
        List<AdminRole> sysRoleList = this.list(srLqw);
        if(sysRoleList != null && sysRoleList.size()>0){
            return R.error("code 不允许重复");
        }
        adminRole.setStatus(StatusEnum.OK.getCode());
        boolean isSuccess = this.save(adminRole);
        if(isSuccess){
            return R.success("新增成功");
        }else{
            return R.error("新增失败");
        }
    }

    @Override
    public R<String> updateRole(AdminRole adminRole) {
        String code = adminRole.getCode();
        if(code == null || StringUtils.isBlank(code)){
            return R.error("code 不允许为空");
        }
        Long id = adminRole.getId();
        LambdaQueryWrapper<AdminRole> srLqw = new LambdaQueryWrapper<>();
        srLqw.eq(AdminRole::getStatus,StatusEnum.OK.getCode());
        srLqw.eq(AdminRole::getCode,code);
        srLqw.ne(AdminRole::getId,id);
        List<AdminRole> sysRoleList = this.list(srLqw);
        if(sysRoleList != null && sysRoleList.size()>0){
            return R.error("code 不允许重复");
        }
        boolean isSuccess = this.updateById(adminRole);
        if(isSuccess){
            return R.success("修改成功");
        }else{
            return R.error("修改失败");
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public R<String> deleteRole(Long id) {
        AdminRole sysRole = this.getById(id);
        sysRole.setStatus(StatusEnum.DELETE.getCode());
        boolean isSuccess = this.updateById(sysRole);

        // 删除 user_role
        if(isSuccess){
            // 删除
            if(isSuccess){
                LambdaQueryWrapper<AdminRoleMenu> srmLqw = new LambdaQueryWrapper<>();
                srmLqw.eq(AdminRoleMenu::getRoleId,id);
                this.adminRoleMenuService.remove(srmLqw);
            }
        }
        if(isSuccess){
            return R.success("删除成功");
        }else{
            return R.error("删除失败");
        }
    }

    @Override
    public R<String> setRoleMenu(AdminRoleVo adminRoleVo) {
        //先删除
        Long roleId = adminRoleVo.getId();
        LambdaQueryWrapper<AdminRoleMenu> srmLqw = new LambdaQueryWrapper<>();
        srmLqw.eq(AdminRoleMenu::getRoleId,roleId);
        this.adminRoleMenuService.remove(srmLqw);

        //List<SysMenuVo> sysMenuVoList = sysRoleVo.getSysMenuVoList();
        List<Long> sysMenuIdList = adminRoleVo.getSysMenuIdList() ;
        /*for(SysMenuVo sysMenuVo:sysMenuVoList){
            sysMenuIdList.add(sysMenuVo.getId());
        }*/
        List<AdminRoleMenu> sysRoleMenuList = new ArrayList<>();
        for(Long sysMenuId : sysMenuIdList){
            AdminRoleMenu sysRoleMenu = new AdminRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(sysMenuId);
            sysRoleMenuList.add(sysRoleMenu);
        }
        Boolean isSuccess = this.adminRoleMenuService.saveBatch(sysRoleMenuList);
        if(isSuccess){
            return R.success("设置成功");
        }else{
            return R.error("设置失败");
        }
    }

    /**
     * 查询菜单树通过 RoleId
     * @param id
     * @return
     */
    @Override
    public R<Map<String,Object>> selectMenuTreeByRoleId(Long id) {
        LambdaQueryWrapper<AdminMenu> smLqw = new LambdaQueryWrapper<>();
        smLqw.eq(AdminMenu::getStatus,StatusEnum.OK.getCode());
        List<AdminMenu> sysMenuList = this.adminMenuService.list(smLqw);
        LambdaQueryWrapper<AdminRoleMenu> armLqw = new LambdaQueryWrapper<>();
        armLqw.eq(AdminRoleMenu::getRoleId,id);
        List<AdminRoleMenu> adminRoleMenuList = this.adminRoleMenuService.list(armLqw);
        List<Long> adminMenuIdList  = adminRoleMenuList.stream().map(AdminRoleMenu::getMenuId).collect(Collectors.toList());

        List<AdminMenuTree> sysMenuTreeList = this.adminMenuService.selectMenuTree(null, adminMenuIdList);
        Integer size = sysMenuList == null ? 0 : sysMenuList.size();
        Integer checkedSize = adminMenuIdList == null ? 0 : adminMenuIdList.size();
        Map<String,Object> returnData = new HashMap<>();
        returnData.put("treeList",sysMenuTreeList);
        returnData.put("treeSize",size);
        returnData.put("checkedSize",checkedSize);
        return R.success(returnData);
    }

    @Override
    public R<List<AdminRole>> selectRoleList() {
        LambdaQueryWrapper<AdminRole> arLqw = new LambdaQueryWrapper<>();
        arLqw.eq(AdminRole::getStatus,StatusEnum.OK.getCode());
        List<AdminRole> adminRoleList = this.list(arLqw);
        return R.success(adminRoleList);
    }
}




